package com.fjnldx.mes.service.common;

import com.fjnldx.mes.domain.domainReq.GetDataDicList;
import com.fjnldx.mes.domain.domainReq.QueryParamReq;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.domainRes.ResultObject;
import com.fjnldx.mes.domain.pojo.*;
import com.fjnldx.mes.exception.ProcessException;
import com.fjnldx.mes.mapper.common.CommonMapper;
import com.fjnldx.mes.mapper.dataDictionary.DataDictionaryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class CommonService {
    @Resource
    CommonMapper commonMapper;
    @Resource
    DataDictionaryMapper dataDictionaryMapper;

    /**
     * 测试测试
     */
    public Result test() throws Exception {
        try {
            List<Map<String, Object>> test = commonMapper.test();
            return Result.success(test);
        } catch (Exception e) {
            throw new Exception();
        }
    }
    /**
     * 顶部查询栏调用数据字典
     */
    public Result queryDataDictionaryList(GetDataDicList bh) throws Exception {
        try {
            List<FzyDictionaryDataList> list = dataDictionaryMapper.queryDataDictionaryList(new FzyDictionaryDataList(bh.getDic_bh(), -1));
            ResultObject<FzyDictionaryDataList> res = new ResultObject<>();
            res.setDataList(list);
            return Result.success(res);
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 用户登录
     *
     * @return Result
     */
    public Result usersLogin(User user) {
        Result result = new Result();
        try {
            List<User> users = commonMapper.usersLogin(user);
            if (users.size() > 0) {
                result.setCode(1);
                result.setStatus("success");
                result.setData(users);
                result.setMsg("成功");
            } else {
                throw new ProcessException("未注册，或账号密码错误！");
            }
        } catch (Exception e) {
            result.setCode(-1);
            result.setStatus("error");
            result.setMsg(e.getMessage());
        }
        return result;
    }

    /**
     * 用户注册
     *
     * @return Result
     */
    public Result userRegister(User user) {
        Result result = new Result();
        int i = 0;
        try {
            User isRegister = commonMapper.isRegister(user);
            if (isRegister != null) {
                throw new ProcessException("用户已注册");
            }
            i = commonMapper.userRegister(user);
            result.setCode(1);
            result.setStatus("success");
            result.setMsg("注册成功");
        } catch (Exception e) {
            result.setCode(-1);
            result.setStatus("error");
            result.setMsg(e.getMessage());
        }
        return result;
    }

    /**
     * 更新用户信息
     *
     * @return Result
     */
    public Result updateUserInfo(User user) {
        Result result = new Result();
        int i = 0;
        try {
            i = commonMapper.updateUserInfo(user);
            result.setCode(1);
            result.setStatus("success");
            result.setMsg("更新成功");
        } catch (Exception e) {
            result.setCode(-1);
            result.setStatus("error");
            result.setMsg("更新失败");
        }
        return result;
    }

    /**
     * 注销用户
     *
     * @return Result
     */
    public Result deleteUser(Integer id) {
        Result result = new Result();
        int i = 0;
        try {
            if (id == 1 || id == 2) {
                throw new ProcessException("管理员账号无法删除");
            } else {
                i = commonMapper.deleteUser(id);
                result.setCode(1);
                result.setStatus("success");
                result.setMsg("注销成功");
            }
        } catch (Exception e) {
            result.setCode(-1);
            result.setStatus("error");
            result.setMsg("注销失败 " + e.getMessage());
        }
        return result;
    }

    /**
     * 查询参数值
     *
     * @param queryParamReq
     * @return
     */
    public Result queryParam(QueryParamReq queryParamReq) {
        List<QueryParam> queryParamList = commonMapper.queryParam(queryParamReq);
        return Result.success(queryParamList);
    }
}
